<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Models\Adverts;
class AdvertsController extends Controller
{
    use ModelForm;
    protected $headerStr = '公告';

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header($this->headerStr . '管理');
            $content->body($this->grid());
        });

    }

    protected function form()
    {
        return Admin::form(Adverts::class, function (Form $form) {
            $form->display('id', 'ID:');
            $form->text('title', '标题:');
            $form->ckeditor('content', '内容:');
            $form->tools(function (Form\Tools $tools) {
                $tools->disableBackButton();// 去掉返回按钮
                // $tools->disableListButton(); // 去掉跳转列表按钮
                //$tools->disableDelete(); // 去掉跳转列表按钮
                if(method_exists('tools','disableView')){
                    $tools->disableView();
                }
            });
        });
    }

    protected function grid()
    {
        return Admin::grid(Adverts::class, function (Grid $grid) {
            $grid->id('编号')->sortable();
            $grid->title('标题');
            $grid->created_at('创建时间');
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->perPages([10, 20, 30, 40, 50]);
            $grid->actions(function ($actions) {
                //$actions->disableDelete();
                if(method_exists($actions,'disableView')){
                    $actions->disableView();
                }
            });
            $grid->paginate(env('Admin_Limit'));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form());
        });
    }

    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form()->edit($id));
        });
    }
}